package com.itheima.threadstructuredemo;

import java.util.List;
import java.util.concurrent.Callable;

public class ThreadGetRedPacket implements Runnable {
    private Employee employee;
    private static int idx = 0;
    public static List<Integer> redPackets;

    public ThreadGetRedPacket(Employee employee) {
        this.employee = employee;
    }

    @Override
    public void run(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            synchronized (ThreadGetRedPacket.class){
                if (idx < redPackets.size()) {
                    System.out.println(employee.getName() + "抢到了" + idx + "号红包，金额为" + redPackets.get(idx) + "元红包");
                    employee.setGetRedPacketNum(employee.getGetRedPacketNum() + redPackets.get(idx));
                }
                else break;
                idx++;
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
